/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */




package learn.round1A;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.Scanner;

/**
 *
 * @author robit
 */
public class Join_K1 {

    Join_K1(){}

    private static Scanner s = null;
    private static PrintWriter o = null;
    /**
    * calculate the answer
    * 3 ≤ K ≤ N.
    * 3 ≤ N ≤ 50.
    */
    private static String reckon() {
        int N=Int();
        int K=Int();

        char board[][]= new char[N][];
        int firstnonblank=0;
        for(int line=0;line<N;line++){
            String str=String();
            CharSequence a=null;
            if(!str.contains("R")&&!str.contains("B"))firstnonblank=line+1;
            board[line]=str.toCharArray();
        }

        System.out.println("Formerly");
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                System.out.print(board[i][j]);
            }
            System.out.println();
        }
        
        int nonblank=N-firstnonblank;
        //System.out.println(firstnonblank);

        char indent[][]=new char[nonblank][N];
        for(int i=firstnonblank;i<N;i++){
                int idx=N-1;
                for(int j=N-1;j>=0;j--){
                     switch(board[i][j]){
                        case 'R':indent[i-firstnonblank][idx--]='R';break;
                        case 'B':indent[i-firstnonblank][idx--]='B';break;
                     }
                }
                while(idx>=0)indent[i-firstnonblank][idx--]='.';
        }
        
        System.out.println("Modified");
        for(int i=0;i<nonblank;i++){
            for(int j=0;j<N;j++){
                System.out.print(indent[i][j]);
            }
            System.out.println();
        }

        int max_i=nonblank-1,max_j=N-1;
        int Rcount=0,Bcount=0;
        boolean Rflag=false,Bflag=false;
        int log[][]=new int[nonblank][N];
        //type 1
        for (int c = 0; c <=max_i+max_j; c++) {
            for(int i=(c<max_i)?c:max_i;i>=((c-max_j>0)?c-max_j:0);i--){
                if(Rflag&&Bflag)break;

                int j=c-i;
                switch(indent[i][j]){
                    case 'R':
                        Rcount++;Bcount=0;log[i][j]=Rcount;
                        break;
                    case 'B':
                        Bcount++;Rcount=0;log[i][j]=Bcount;
                        break;
                    case '.':
                        Rcount=0;Bcount=0;log[i][j]=0;
                        break;
                }

                Rflag=Rflag||(Rcount>=K);
                Bflag=Bflag||(Bcount>=K);
                //indent[i][j]=(char)(c+'A');
            }
            Rcount=Bcount=0;
        }
        System.out.println("Diagonalize_1");
        for(int i=0;i<nonblank;i++){
            for(int j=0;j<N;j++){
                System.out.print(log[i][j]);
            }
            System.out.println();
        }

        
        //type 2
        for (int c = max_i; c >=-max_j; c--) {
            for(int i=(c>0)?c:0;i<=((max_i<max_j+c)?max_i:max_j+c);i++){
                if(Rflag&&Bflag)break;

                int j=i-c;
                switch(indent[i][j]){
                    case 'R':
                        Rcount++;Bcount=0;log[i][j]=Rcount;
                        break;
                    case 'B':
                        Bcount++;Rcount=0;log[i][j]=Bcount;
                        break;
                    case '.':
                        Rcount=0;Bcount=0;log[i][j]=0;
                        break;
                }

                Rflag=Rflag||(Rcount>=K);
                Bflag=Bflag||(Bcount>=K);
                //indent[i][j]=(char)(max_i-c+'A');
            }
            Rcount=Bcount=0;
        }
        System.out.println("Diagonalize_2");
        for(int i=0;i<nonblank;i++){
            for(int j=0;j<N;j++){
                System.out.print(log[i][j]);
            }
            System.out.println();
        }

        //type 3
        for (int i = 0; i<nonblank; i++) {
            for(int j=0;j<N;j++){
                if(Rflag&&Bflag)break;

                switch(indent[i][j]){
                    case 'R':
                        Rcount++;Bcount=0;log[i][j]=Rcount;
                        break;
                    case 'B':
                        Bcount++;Rcount=0;log[i][j]=Bcount;
                        break;
                    case '.':
                        Rcount=0;Bcount=0;log[i][j]=0;
                        break;
                }

                Rflag=Rflag||(Rcount>=K);
                Bflag=Bflag||(Bcount>=K);
                //indent[i][j]=(char)(i+'A');
            }
            Rcount=Bcount=0;
        }
        System.out.println("Horizontal");
        for(int i=0;i<nonblank;i++){
            for(int j=0;j<N;j++){
                System.out.print(log[i][j]);
            }
            System.out.println();
        }

        //type 4
        for(int j=0;j<N;j++){
            for (int i = 0; i<nonblank; i++) {
                if(Rflag&&Bflag)break;

                switch(indent[i][j]){
                    case 'R':
                        Rcount++;Bcount=0;log[i][j]=Rcount;
                        break;
                    case 'B':
                        Bcount++;Rcount=0;log[i][j]=Bcount;
                        break;
                    case '.':
                        Rcount=0;Bcount=0;log[i][j]=0;
                        break;
                }

                Rflag=Rflag||(Rcount>=K);
                Bflag=Bflag||(Bcount>=K);
                //indent[i][j]=(char)(j+'A');
            }
            Rcount=Bcount=0;
        }
        System.out.println("Vertical");
        for(int i=0;i<nonblank;i++){
            for(int j=0;j<N;j++){
                System.out.print(log[i][j]);
            }
            System.out.println();
        }
        System.out.println("End case");
        if(Rflag&&Bflag)return "Both";
        else if(Rflag)return "Red";
        else if(Bflag)return "Blue";
        else return "Neither";
    }


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        
        try {
            //String filename="test";
            String prob="A";
            //String filename=prob+"-small-practice";
            String filename=prob+"-large-practice";

            s = new Scanner(
                    new BufferedReader(new FileReader(filename+".in")));
            //s.useLocale(Locale.US);
            o = new PrintWriter(
                    new BufferedWriter(new FileWriter(filename+".out")));

            // Determine input line number
            int lineNums = s.nextInt();

            for (int i = 1; i <= lineNums; i++) {
                o.println("Case #" + i + ": " + reckon());
            }
        } finally {
            s.close();
            o.close();
        }
    }


    private static int Int(){
        return s.nextInt();
    }
    private static long Long(){
        return s.nextLong();
    }
    private static String String(){
        return s.next();
    }

}
